// app.js
import websocket from './utils/socket'
import api from "./utils/api";
App({
  onLaunch(options) {
    const {
      query = {}
    } = options || {};
    console.log('query: app', query);
    // this.socketFun();
    this.autoUpdate();
    this.checkFullSucreen();
    this.addGlobalData(query);
  },
  onShow() {
    this.commonOauthBaseinfo();
  },
  onPageNotFound() {
    wx.redirectTo({
      url: "pages/404/404",
    }); //
  },
  // socketFun() {
  //   websocket({key: 'app'}, (res) => {
  //     console.log("WebSocket app.ts ===>", res);
  //     const data = JSON.parse(res)
  //     if (data.msg && Object.keys(data.msg).length > 0 && this.globalData.currentPage !== 'customerIntelligent') {
  //       wx.showTabBarRedDot({
  //         index: 1
  //       });
  //     }
  //   });
  // },
  autoUpdate() {
    // 获取小程序更新机制兼容
    if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager();
      //1. 检查小程序是否有新版本发布
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log("res: onCheckForUpdate", res);
      });
      updateManager.onUpdateReady(() => {
        wx.showModal({
          title: "更新提示",
          content: "发现新版本，请重启应用",
          success: function (res2) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            res2.confirm && updateManager.applyUpdate();
          },
        });
      });
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: "提示",
        content: "微信版本过低，请升级到最新微信版本后重试。",
        success: function (res) {
          if (res.confirm) {
            wx.updateWeChatApp();
          }
        },
      });
    }
  },
  globalData: {
    isFullSucreen: false,
    requestMessage: {},
    query: {}, // 链接参数
    currentPage: '',
    customerChoose: {}
  },

  /**
   * 判断设备是否为全面屏
   */
  checkFullSucreen: function () {
    const self = this;
    wx.getSystemInfo({
      success: function (res) {
        // 根据 屏幕高度 进行判断
        if (
          res.screenHeight - res.windowHeight - res.statusBarHeight - 32 >
          72
        ) {
          self.globalData.isFullSucreen = true;
        }
        // 根据手机型号匹配
        if (res.model.search("iPhone X") != -1) {
          self.globalData.isFullSucreen = true;
        }
      },
    });
  },

  commonOauthBaseinfo() {
    console.log("commonOauthBaseinfo");
    api.commonOauthBaseinfo({
      // 调用接口，传入参数
      data: {},
      success: (res) => {
        console.log(res, "commonOauthBaseinfo========================");
        if (res.code * 1 === 0 && res.data.status * 1 === 2) {
          wx.showToast({
            title: "账号异常，请更换账号后登录",
            icon: "none",
          });
          wx.clearStorage({
            success: (res) => {
              wx.switchTab({
                url: "../../index/index",
              });
            },
          });
        }
        if (res.code * 1 === 1) {
          wx.showToast({
            title: "请重新登录",
            icon: "none",
          });
          wx.clearStorage({
            success: (res) => {
              wx.switchTab({
                url: "../../index/index",
              });
            },
          });
        }
        const {
          channel
        } = this.globalData;
        if (!channel) {
          this.globalData.channel =
            res && res.data && res.data.channel ? res.data.channel : 0;
        }
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },

  /**
   * @description: 添加全局query
   * @param {*} query 入参
   * @return {*}
   */
  addGlobalData(query = {}) {
    const sceneObj = {};
    if (query.scene) {
      const scenestr = decodeURIComponent(query.scene);
      const scene = scenestr.split("&");
      scene.forEach((item) => {
        if (item) {
          let sceneItem = item.split("=");
          sceneObj[sceneItem[0]] = sceneItem[1];
        }
      });
    }
    this.globalData = {
      ...this.globalData,
      ...query,
      ...sceneObj,
    };
  },
});